package com.LA_houseLease.admin.controller.apartment;

import com.LA_houseLease.admin.service.PaymentTypeService;
import com.LA_houseLease.common.result.Result;
import com.LA_houseLease.pojo.entity.PaymentType;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin(origins = "*")
@Tag(name = "支付方式管理")
@RequestMapping("/admin/payment")
@RestController
@Slf4j
public class PaymentTypeController {

    @Autowired
    private PaymentTypeService paymentTypeService;

    @Operation(summary = "查询全部支付方式列表")
    @GetMapping("list")
    public Result<List<PaymentType>> listPaymentType() {
        log.warn("查询全部支付方式列表-------PaymentTypeController.listPaymentType");
        /**
         * 添加过滤条件，过滤is_deleted
         * 在此使用LambdaQueryWrapper
         * 后续我们直接使用mybatis-plus中提供的逻辑删除策略即可
         */
        //LambdaQueryWrapper<PaymentType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //lambdaQueryWrapper.eq(PaymentType::getIsDeleted,0);
        //List<PaymentType> list = paymentTypeService.list(lambdaQueryWrapper);

        List<PaymentType> list = paymentTypeService.list();
        return Result.ok(list);
    }

    @Operation(summary = "保存或更新支付方式")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdatePaymentType(@RequestBody PaymentType paymentType) {
        log.warn("保存或更新支付方式-------PaymentTypeController.saveOrUpdatePaymentType");
        paymentTypeService.saveOrUpdate(paymentType);   //mybatis-plus这个saveOrUpdate方法会在id为null时执行save,在id!=null时执行update
        return Result.ok();
    }

    @Operation(summary = "根据ID删除支付方式")
    @DeleteMapping("deleteById")
    public Result deletePaymentById(@RequestParam Long id) {
        //因为我们已经在yml文件中配过了MP逻辑删除,所以此处虽然我们执行的是MP的删除语句但实际执行的SQL语句是更新操作，即逻辑删除
        paymentTypeService.removeById(id);
        return Result.ok();
    }

}















